<template>
  <div class="app-container">
    <el-table v-loading="listLoading" :data="list" height="400" style="width: 100%;margin-top:10px; overflow:auto" border>
      <el-table-column label="序号" type="index" header-align="center" align="center" width="60" />
      <el-table-column header-align="center" align="left" label="操作" width="120">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="download(scope)">下载</el-button>
          <el-button v-if="['png','jpeg','jpg','pdf'].includes(scope.row.suffix)" type="text" size="small" @click="yulan(scope)">预览</el-button>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="left" label="文档名" show-overflow-tooltip min-width="520px">
        <template slot-scope="scope">
          <span>{{ scope.row.originalName }}</span>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="left" label="文档类型" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.suffix }}</span>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="left" label="目录" width="240">
        <template slot-scope="scope">
          <span>{{ scope.row.xftName }}</span>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="right" label="版本号" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.sortNum }}</span>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="left" label="创建人员" width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.createuserName }}</span>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="right" label="创建时间" width="180">
        <template slot-scope="scope">
          {{ scope.row.createtimeTi }}
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="viewPhotoVisible" append-to-body width="640px" title="预览">
      <view-photo v-if="viewPhotoVisible" ref="viewPhoto" :proid="code" :visible.sync="viewPhotoVisible" />
    </el-dialog>

    <el-dialog :visible.sync="viewPdfVisible" append-to-body width="1200px" title="预览">
      <view-pdf v-if="viewPdfVisible" ref="viewPdf" :proid="code" :visible.sync="viewPdfVisible" />
    </el-dialog>
  </div>
</template>

<script>

import { getHistory, downloadFile, saveRecords } from '@/api/projectFile'

import ViewPhoto from '../zlgl/viewPhoto' // eslint-disable-line no-unused-vars
import ViewPdf from '../zlgl/viewPdf'
export default {
  components: { ViewPhoto, ViewPdf },
  inject: ['getList'],
  props: { // 第二种方式
    history: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      listLoading: false,
      viewPhotoVisible: false,
      viewPdfVisible: false,
      list: []
    }
  },
  created() {
    this.loadList()
  },
  methods: {
    loadList() { // 加载列表
      this.list = []

      getHistory({ batchCode: this.history }).then(response => {
        const { data } = response

        this.list = data
        this.listLoading = false
      }).catch(response => {
        this.listLoading = false
      })
    },
    yulan(scope) {
      saveRecords({ fileId: scope.row.id, flag: '1' }).then(response => {
      })
      if (['png', 'jpeg', 'jpg', 'PNG', 'JPEG', 'JPG'].includes(scope.row.suffix)) {
        this.viewPhotoVisible = true
        this.code = scope.row.id
        this.$nextTick(() => {
          this.$refs.viewPhoto
        })
      } else if (['pdf', 'PDF'].includes(scope.row.suffix)) {
        this.viewPdfVisible = true
        this.code = scope.row.id
        this.$nextTick(() => {
          this.$refs.viewPdf
        })
      }
    },
    download(scope) {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      downloadFile({ id: scope.row.id }).then(response => {
        var fileName = scope.row.originalName
        const contentDisposition = response.headers['content-disposition']
        if (contentDisposition) {
          fileName = window.decodeURI(response.headers['content-disposition'].split('=')[1], 'UTF-8')
        }
        const blob = new Blob([response.data], {
          type: `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` // word文档为msword,pdf文档为pdf
        })
        const objectUrl = URL.createObjectURL(blob)
        const link = document.createElement('a')
        link.href = objectUrl
        link.setAttribute('download', fileName)
        document.body.appendChild(link)
        link.click()
        // 释放内存
        window.URL.revokeObjectURL(link.href)

        saveRecords({ fileId: scope.row.id, flag: '2' }).then(response => {
        })
        loading.close()
      }).catch(response => {
        loading.close()
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
